-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====
 
LocationDBEditor = {};

SelectedLocationSettings = {}
SelectedLocationSettings["SHOW_GUILDS"] = 1;

function LocationDBEditor:showLocation( location )

    LocationDBEditorFrame_ZoneKeyValue:SetText( location:getKey() );
    LocationDBEditorFrame_ContinentValue:SetText( location:getContinent() );
    LocationDBEditorFrame_ZoneValue:SetText( location:getZone() );
    LocationDBEditorFrame_SubZoneValue:SetText( location:getSubZone() );
    LocationDBEditorFrame_XMapPositionValue:SetText( location:getXMapCoord() );
    LocationDBEditorFrame_YMapPositionValue:SetText( location:getYMapCoord() );
    LocationDBEditorFrame_PositionRadiusValue:SetText( location:getDistanceToMapCoord() );
    LocationDBEditorFrame_ZoneDescValue:SetText( location:getInfo() );
    
end

function LocationDBEditor:CaptureRadius()

    local refX = tonumber( LocationDBEditorFrame_XMapPositionValue:GetText() );
    local refY = tonumber( LocationDBEditorFrame_YMapPositionValue:GetText() );
    
    local posX, posY = GetPlayerMapPosition("player");
        
    local radius = Location:MapFloor( math.sqrt ( ( posX - refX ) * ( posX - refX ) + ( posY - refY ) * ( posY - refY ) ) );
        
    LocationDBEditorFrame_PositionRadiusValue:SetText( radius );
    
end

function LocationDBEditor:captureZone()

    local location = Location:new();
    location:capturePlayerZone();
    self:showLocation( location );
    
end



function LocationDBEditor:LoadZone()

    --SBMessageManager:debugMessage( "function LocationDBEditor:LoadZone()" );
    local tmpLocation = Location:new(); 
    --SBMessageManager:debugMessage( tmpLocation:export() );
    self:showLocation( tmpLocation );
    
--    if ( self.db.profile.location ~= nil )
--    then
--        mySavedLocation:import( self.db.profile.location ); 
--    end

    
end

function LocationDBEditor:SaveZone()

    local myEditedLocation = Location:new();
    
    -- get values
    myEditedLocation:setKey( LocationDBEditorFrame_ZoneKeyValue:GetText() );
    myEditedLocation:setContinent( LocationDBEditorFrame_ContinentValue:GetText() );
    myEditedLocation:setZone( LocationDBEditorFrame_ZoneValue:GetText() );
    myEditedLocation:setSubZone( LocationDBEditorFrame_SubZoneValue:GetText() );
    myEditedLocation:setXMapCoord( tonumber(LocationDBEditorFrame_XMapPositionValue:GetText()) );
    myEditedLocation:setYMapCoord( tonumber(LocationDBEditorFrame_YMapPositionValue:GetText()) );
    myEditedLocation:setDistanceToMapCoord( tonumber(LocationDBEditorFrame_PositionRadiusValue:GetText()) );
    myEditedLocation:setInfo( LocationDBEditorFrame_ZoneDescValue:GetText() );
    
    -- save values
    --self.db.profile.location = myEditedLocation:export();
    StoryBoard.currentStoryline.locationDB:addLocation( myEditedLocation );
    
end



function LocationDBEditor:DropDownMenuHandler()

    --SBMessageManager:debugMessage( "MyDropDownMenu_FunctionCalledWhenOptionIsClicked");
    UIDropDownMenu_SetSelectedID(MyDropDownMenuButton, this:GetID());
    
    local selectionIndex = UIDropDownMenu_GetSelectedID(MyDropDownMenuButton);
    SelectedLocationSettings["SHOW_GUILDS"] = selectionIndex;
    --SBMessageManager:debugMessage( UIDropDownMenu_GetSelectedID(MyDropDownMenuButton) );
    --SBMessageManager:debugMessage( LocationDBEditor.dropDownMenuVector[ UIDropDownMenu_GetSelectedID(MyDropDownMenuButton)] );
    
    self.locationKey = LocationDBEditor.dropDownMenuVector[ selectionIndex ];
    local selectedLocation = StoryBoard.currentStoryline.locationDB.db[ self.locationKey ];
    self:showLocation( selectedLocation );
    
end

function LocationDBEditor_Handler()
    LocationDBEditor:DropDownMenuHandler();
end

function LocationDBEditor_DropDownMenuOnLoad()
    LocationDBEditor:DropDownMenuOnLoad();
end

function LocationDBEditor:DropDownMenuOnLoad()

    self.dropDownMenuVector = {}

    local info = {};
    info.func = LocationDBEditor_Handler;
    
    if( StoryBoard.db ~= nil and
        StoryBoard.currentStoryline ~= nil )
    then
        local cmpt = 0;
        for k,v in pairs( StoryBoard.currentStoryline.locationDB.db ) do
            info.text = k;
            cmpt = cmpt + 1;
            info.checked = SelectedLocationSettings["SHOW_GUILDS"] == cmpt;
            UIDropDownMenu_AddButton(info);
            table.insert( LocationDBEditor.dropDownMenuVector, k );
        end
    end
    
end

--function MyDropDownMenuButton_OnClick() 
 --      ToggleDropDownMenu(1, nil, MyDropDownMenu, MyDropDownMenuButton, 0, 0);
--end

function LocationDBEditor:EraseZone()

    StoryBoard.currentStoryline.locationDB:removeLocation( self.locationKey );
    
    LocationDBEditor:DropDownMenuOnLoad();
    
    self.selectionIndex = 1;
    self.locationKey = self.dropDownMenuVector[ self.selectionIndex ];
    
    UIDropDownMenu_SetSelectedID( MyDropDownMenuButton, self.selectionIndex );
    UIDropDownMenu_SetText( MyDropDownMenuButton, self.locationKey );
    
    local selectedLocation = StoryBoard.currentStoryline.locationDB.db[ self.locationKey ];
    self:showLocation( selectedLocation );
    
end
